package com.yequan.hbb.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.yequan.hbb.app.MyApplication;
import com.yequan.hbb.database.MyOpenHelper;

/**
 * Created by Administrator on 2016/10/13.
 */

public class MyContentProvider extends ContentProvider {
    private MyOpenHelper openHelper;
    private static UriMatcher uriMatcher;
    private final static String AUTHORITY = "hbbContentProvider.authority";
    private final static int USER = 0;

    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY, "user", USER);
    }

    @Override
    public boolean onCreate() {
        openHelper = new MyOpenHelper(getContext());
        return true;
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteDatabase writableDatabase = openHelper.getWritableDatabase();
        int match = uriMatcher.match(uri);
        Cursor cursor = null;
        switch (match) {
            case USER:
                cursor = writableDatabase.query("user", projection, selection, selectionArgs, null, null, sortOrder);
                break;
        }
        writableDatabase.close();
        return cursor;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        int match = uriMatcher.match(uri);
        switch (match) {
            case USER:
                return "com.yequan.hbbContentProvider/user";
            default:
                throw new IllegalArgumentException("this is unkown uri:" + uri);
        }
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase writableDatabase = openHelper.getWritableDatabase();
        int match = uriMatcher.match(uri);
        long insert = 0;
        switch (match) {
            case USER:
                insert = writableDatabase.insert("user", null, values);
                if (insert > 0) {
                    Toast.makeText(getContext(), "注册成功", Toast.LENGTH_SHORT).show();
                    MyApplication.handler.sendEmptyMessage(100);
                } else {
                    Toast.makeText(getContext(), "注册失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }
        writableDatabase.close();
        return ContentUris.withAppendedId(uri, insert);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase writableDatabase = openHelper.getWritableDatabase();
        int match = uriMatcher.match(uri);
        int delete = 0;
        switch (match) {
            case USER:
                delete = writableDatabase.delete("user", selection, selectionArgs);
                break;
        }
        writableDatabase.close();
        return delete;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        SQLiteDatabase writableDatabase = openHelper.getWritableDatabase();
        int match = uriMatcher.match(uri);
        int update = 0;
        switch (match) {
            case USER:
                update = writableDatabase.update("user", values, selection, selectionArgs);
                break;
        }
        writableDatabase.close();
        return update;
    }
}
